package io.rong.app.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.yunshipei.base.BaseActivity;
import com.yunshipei.base.BaseSubscriberAdapter;
import com.yunshipei.common.net.HttpMethods;
import com.yunshipei.common.wedigt.YspTitleBar;
import com.yunshipei.enterplorer.R;

import org.json.JSONObject;

public class RefreshGroupNameActivity extends BaseActivity implements View.OnClickListener {
    public static final String NEWGROUPNAME = "newName";
    public static final int RESULTCODE = 6;
    private EditText newGroupName;
    private String gid;
    private String newName;
    private AlertDialog putImageDialog;

    private YspTitleBar mTitleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh_group_name);
        initView();
        initData();
    }

    private void initView() {
        mTitleBar = (YspTitleBar) findViewById(R.id.title_bar);
        mTitleBar.setSubTitleClickListener(this);
        newGroupName = (EditText) findViewById(R.id.new_group_name);
    }

    private void initData() {
        Intent intent = getIntent();
        gid = intent.getStringExtra(GroupMainPageActivity.GID);
        newGroupName.setHint(intent.getStringExtra(GroupMainPageActivity.GROUPNAME));
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.extras_title:
                newName = newGroupName.getText().toString();
                if (!TextUtils.isEmpty(newName)) {
                    showRoundProcessDialog(this, R.layout.loading_process_dialog_anim);
                    HttpMethods.getInstance().modifyIMGroupName(gid, newName, new EditGroupNameSubscriber());
                } else {
                    Toast.makeText(this, "群名称不能为空", Toast.LENGTH_LONG).show();
                }
                break;
            default:
                break;
        }
    }

    public class EditGroupNameSubscriber extends BaseSubscriberAdapter<JSONObject> {

        @Override
        public void onError(String errorMessage) {
            closeProcessDialog();
        }

        @Override
        public void onNext(JSONObject jsonObject) {
            closeProcessDialog();
            Intent intent = new Intent();
            intent.putExtra(RefreshGroupNameActivity.NEWGROUPNAME, newName);
            setResult(RESULTCODE, intent);
            finish();
        }

    }

    public void showRoundProcessDialog(Context mContext, int layout) {
        DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_SEARCH) {
                    return true;
                }
                return false;
            }
        };

        putImageDialog = new AlertDialog.Builder(mContext).create();
        putImageDialog.setOnKeyListener(keyListener);
        putImageDialog.show();
        putImageDialog.setContentView(layout);
    }

    public void closeProcessDialog() {
        if (putImageDialog.isShowing()) {
            putImageDialog.dismiss();
        }
    }
}
